//
//  StoreCollectionV.m
//  FKGW
//
//  Created by song ce on 2017/2/21.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "StoreCollectionV.h"
#import "BaseNavigationViewController.h"

#import "UIView+Extend.h"
#import "GoodsDetailViewController.h"
#define KColumCount 2
@implementation StoreCollectionV

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self _initSubviews];
    }
    return self;
}

-(void)setDataSoure:(NSMutableArray *)dataSoure{
    _dataSoure = dataSoure;
    [self setNeedsLayout];
    [_collectionV reloadData];
}


-(UIImageView *)noDataIUmageV{
    if(_noDataIUmageV==nil){
        _noDataIUmageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_zanwushuju"]];
        _noDataIUmageV.frame = CGRectMake((Main_Screen_Width-100)/2, (self.size.height-115)/2, 100, 115);
    }
    return _noDataIUmageV;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    if(!self.dataSoure.count){
        return;
    }
    if(self.isStore){
            CGFloat height;
            if(_dataSoure.count%2==0){
                height = ((Main_Screen_Width-15*3)/2+55+13)*_dataSoure.count/2+18;
            }else{
                height =  ((Main_Screen_Width-15*3)/2+55+13)*(_dataSoure.count+1)/2+18;
            }
            self.height = height;
            _collectionV.frame = CGRectMake(0, 0, Main_Screen_Width, height);
    }
}
- (void)_initSubviews{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置每个item的大小
//    layout.itemSize = CGSizeMake(50,50);
    //每一行之间的最小距离
    layout.minimumLineSpacing = 13;
    //同一行item之间的最小间隙
    layout.minimumInteritemSpacing =15;
    //设置滑动方向
//    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionV = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    //设置分页
//    _collectionV.pagingEnabled = YES;
      [self addSubview:_collectionV];
    _collectionV.backgroundColor = [UIColor whiteColor];
//    _collectionV.scrollEnabled = NO;
    //设置代理
    _collectionV.delegate = self;
    _collectionV.dataSource = self;
    //注册
    [_collectionV registerNib:[UINib nibWithNibName:@"StoreCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"StoreCollectionViewCell"];
    
    _collectionV.showsVerticalScrollIndicator = NO;
}

-(void)setIsScrollEnabled:(BOOL)isScrollEnabled{
    _isScrollEnabled = isScrollEnabled;
    if(!self.isScrollEnabled ){
        _collectionV.scrollEnabled = NO;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;{
    return self.dataSoure.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
    StoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StoreCollectionViewCell" forIndexPath:indexPath];
    StoreModel *item = self.dataSoure[indexPath.row];
    cell.model = item;
      return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((Main_Screen_Width-15*3)/KColumCount, (Main_Screen_Width-15*3)/KColumCount+55);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets edge = UIEdgeInsetsMake(15, 15, 15 ,15);
    return edge;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    StoreModel *item = self.dataSoure[indexPath.row];
     GoodsDetailViewController *goodsDetail = [[GoodsDetailViewController alloc]init];
    goodsDetail.vcName = @"商品详情";
    goodsDetail.pid = item.id;
    goodsDetail.goodPic = item.imageName;
    goodsDetail.smodel = item;
//    goodsDetail.isNeedMessageItem = YES;
    [[self secondNavigationController] pushViewController:goodsDetail animated:YES];
}

- (BaseNavigationViewController*)secondNavigationController{
    UIResponder *next = self.nextResponder;
    while (next!=nil) {
        if([next isKindOfClass:[BaseNavigationViewController class]]){
            return (BaseNavigationViewController*)next;
        }
        next = next.nextResponder;
    }
    return nil;
}

@end
